package com.xunjing.order.controller;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xunjing.common.core.utils.DateUtils;
import com.xunjing.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xunjing.common.log.annotation.Log;
import com.xunjing.common.log.enums.BusinessType;
import com.xunjing.common.security.annotation.RequiresPermissions;
import com.xunjing.order.domain.SoOrder;
import com.xunjing.order.service.ISoOrderService;
import com.xunjing.common.core.web.controller.BaseController;
import com.xunjing.common.core.web.domain.AjaxResult;
import com.xunjing.common.core.utils.poi.ExcelUtil;
import com.xunjing.common.core.web.page.TableDataInfo;

/**
 * 订单管理Controller
 *
 * @author zhangyt
 * @date 2024-03-29
 */
@RestController
@RequestMapping("/order")
public class SoOrderController extends BaseController
{
    @Autowired
    private ISoOrderService soOrderService;

/**
 * 查询订单管理列表
 */
@RequiresPermissions("order:order:list")
@GetMapping("/list")
    public TableDataInfo list(SoOrder order)
    {
        startPage();
        LambdaQueryWrapper<SoOrder> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(order.getOrderNo())){
            wrapper.eq(SoOrder::getOrderNo,order.getOrderNo());
        }

        LocalDateTime begin;
        LocalDateTime end ;
        if (order.getTime()==null){
            begin = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);//当天零点
            end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);//当天24点
        }else {
            begin = DateUtils.strToLocalDateTime(order.getTime()[0]+" 00:00:00");
            end = DateUtils.strToLocalDateTime(order.getTime()[1]+" 23:59:59");
        }
        wrapper.ge(SoOrder::getCreateTime, begin);
        wrapper.le(SoOrder::getCreateTime, end);
        List<SoOrder> list = soOrderService.list(wrapper);
        return getDataTable(list);
    }

    /**
     * 导出订单管理列表
     */
    @RequiresPermissions("order:order:export")
    @Log(title = "订单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SoOrder soOrder)
    {
        List<SoOrder> list = soOrderService.selectSoOrderList(soOrder);
        ExcelUtil<SoOrder> util = new ExcelUtil<SoOrder>(SoOrder.class);
        util.exportExcel(response, list, "订单管理数据");
    }

    /**
     * 获取订单管理详细信息
     */
    @RequiresPermissions("order:order:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(soOrderService.getById(id));
    }

    /**
     * 新增订单管理
     */
    @RequiresPermissions("order:order:add")
    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SoOrder soOrder)
    {
        return toAjax(soOrderService.save(soOrder));
    }

    /**
     * 修改订单管理
     */
    @RequiresPermissions("order:order:edit")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SoOrder soOrder)
    {
        return toAjax(soOrderService.updateById(soOrder));
    }

    /**
     * 删除订单管理
     */
    @RequiresPermissions("order:order:remove")
    @Log(title = "订单管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(soOrderService.removeByIds(Arrays.asList(ids)));
    }
}
